
package fr.univ.projet.ihm;

import fr.univ.projet.Jeu;
import fr.univ.projet.Joueur;
import fr.univ.projet.data.Base;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class PanelAdmin extends JPanel {

    private JFrame parent;
    
    /**
     * 
     * @param parent Fenetre parent qui va recevoir ce panel
     */
    public PanelAdmin(JFrame parent) {
        initComponents();
        this.parent = parent;
        Base base = Base.getInstance();
        triDesJeux();
        Joueur j= base.getJoueurConnecte();
        lblNom.setText(j.getNom());      
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTiteEnCours = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstJeu = new javax.swing.JList();
        btnTirage = new javax.swing.JButton();
        lblNom = new javax.swing.JLabel();
        btnDeco = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstNew = new javax.swing.JList();
        btnCreer = new javax.swing.JButton();
        lblTitreNew = new javax.swing.JLabel();
        lblTitreFini = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstFini = new javax.swing.JList();

        lblTiteEnCours.setText("Liste des jeux en cours");

        lstJeu.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(lstJeu);

        btnTirage.setText("Tirage");
        btnTirage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTirageActionPerformed(evt);
            }
        });

        lblNom.setText("jLabel1");

        btnDeco.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        btnDeco.setText("Déconnexion");
        btnDeco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDecoActionPerformed(evt);
            }
        });

        lstNew.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(lstNew);

        btnCreer.setText("Créer");

        lblTitreNew.setText("Organiser un nouveau jeu");

        lblTitreFini.setText("Liste des tirage effectués");

        lstFini.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane3.setViewportView(lstFini);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblNom)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDeco)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblTiteEnCours)
                        .addGap(90, 90, 90))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblTitreNew)
                        .addGap(70, 70, 70))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnCreer)
                        .addGap(134, 134, 134))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnTirage)
                        .addGap(130, 130, 130))))
            .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addComponent(lblTitreFini)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
                        .addComponent(jScrollPane1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDeco)
                    .addComponent(lblNom))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblTitreNew)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCreer)
                .addGap(18, 18, 18)
                .addComponent(lblTiteEnCours)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnTirage)
                .addGap(27, 27, 27)
                .addComponent(lblTitreFini)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(77, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnTirageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTirageActionPerformed
       
        Jeu jeuSelectionne = (Jeu) lstJeu.getSelectedValue();
        
        jeuSelectionne.tirage();
        
        triDesJeux();
    }//GEN-LAST:event_btnTirageActionPerformed
    
    /**
     * Action sur le bouton Deconnexion
     * 
     * @param evt 
     */
    private void btnDecoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDecoActionPerformed
        parent.setContentPane(new PanelConnexion(parent));
        parent.pack();
    }//GEN-LAST:event_btnDecoActionPerformed

    /**
     * Affiche les jeux dans la bonne "JList" en fonction de leur état (fini ou en cours)
     */
    private void triDesJeux()
    {
        Base base = Base.getInstance();
        List<Jeu> listeJeux = base.getLstJeu();
        
        List<Jeu> listEnCours = new ArrayList<Jeu>();
        List<Jeu> listFini = new ArrayList<Jeu>();
        
        for( Jeu vJeu : listeJeux)
        {
            if(vJeu.isTirageFait())
                listFini.add(vJeu);
            else
                listEnCours.add(vJeu);
 
        }
        
        lstJeu.setListData(listEnCours.toArray());
        lstFini.setListData(listFini.toArray());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCreer;
    private javax.swing.JButton btnDeco;
    private javax.swing.JButton btnTirage;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lblNom;
    private javax.swing.JLabel lblTiteEnCours;
    private javax.swing.JLabel lblTitreFini;
    private javax.swing.JLabel lblTitreNew;
    private javax.swing.JList lstFini;
    private javax.swing.JList lstJeu;
    private javax.swing.JList lstNew;
    // End of variables declaration//GEN-END:variables
}
